/*
 * Copyright (c) 2010, 2012 Luan Nguyen Thanh. All rights reserved.
 * 
 * This code is written by Luan Nguyen Thanh, with any partners involved.
 * The unauthorized use of this code should not be permitted!
 */
package MyIM.client.view.component;

import MyIM.client.engine.Engine;
import MyIM.client.view.form.Dialog_About;
import MyIM.client.view.form.Dialog_Conference;
import MyIM.client.view.form.Dialog_Login;
import MyIM.client.view.form.Dialog_UserInfo;
import MyIM.utilities.Util;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

/**
 * The ToolBar of home-screen.
 *
 * @author Luan Nguyen Thanh <tklarryonline@gmail.com>
 */
public class MainMenuBar extends JMenuBar implements ActionListener {

    private JMenu menuNewMessage;
    private String currentUser;
    private JRadioButtonMenuItem rbAvailable, rbOffline;
    private Icon icoDummy, icoAvail, icoOff;
    private ActionListener exitListener;
    private Engine engine;

    public MainMenuBar(Engine engine, String currentUser,
            ActionListener exitListener) {
        this.engine = engine;
        this.currentUser = currentUser;
        this.exitListener = exitListener;

        init();
    }

    public MainMenuBar() {
        this.currentUser = "tklarryonline";

        init();
    }

    private void init() {
        icoDummy = Util.makeIcon("dummy_1x1");
        icoAvail = Util.makeIcon("available");
        icoOff = Util.makeIcon("offline");

        menuNewMessage = makeAddConferenceMenu();

//        this.add(makeName());
        this.add(Box.createHorizontalGlue());
        this.add(menuNewMessage);
        this.add(makeUserControlMenu());
        this.add(makeSettingsMenu());
    }

    private JMenu makeAddConferenceMenu() {
        JMenu menu = new JMenu("Conference");
        menu.setIcon(Util.makeIcon("conference"));
        menu.setIconTextGap(4);
        menu.addMenuListener(new MenuListener() {

            @Override
            public void menuSelected(MenuEvent e) {
                startConference();
            }

            @Override
            public void menuDeselected(MenuEvent e) {
            }

            @Override
            public void menuCanceled(MenuEvent e) {
            }
        });

        return menu;
    }

    private JMenu makeSettingsMenu() {
        // Index of the item
        int index = 0;

        JMenu menu = new JMenu();

        menu.setIcon(Util.makeIcon("setting"));

        // Adds 'Help' menu item
//        menu.add(makeMenuItem("Get some help", KeyEvent.VK_H, KeyEvent.VK_F1, 0,
//                "help"));
//        index++;

        // Adds 'about this small piece of app...' menu item
        JMenuItem conference = makeMenuItem("Start conference",
                KeyEvent.VK_C,
                0, 0, "conference");
//        conference.addActionListener(this);
        menu.add(conference);
        index++;

        // Adds 'about this small piece of app...' menu item
        menu.addSeparator();
        JMenuItem about = makeMenuItem("About this small piece of app...",
                KeyEvent.VK_A,
                0, 0, "about");
//        about.addActionListener(this);
        menu.add(about);
        index++;

        // Adds 'Exit' menu item
        menu.addSeparator();
        JMenuItem exit = makeMenuItem("Exit", KeyEvent.VK_X,
                KeyEvent.VK_X, KeyEvent.ALT_DOWN_MASK, "exit");
//        exit.addActionListener(this);
        menu.add(exit);
        index++;

        return menu;
    }

    /**
     *
     */
    private JMenu makeUserControlMenu() {
        // Index of the item
        int index = 0;

        // Sets up components
        JMenu menu = new JMenu(currentUser);
        JMenuItem item;
        menu.setIcon(new ImageIcon(engine.getClient().getAvatar().getImage().
                getScaledInstance(24, 24, Image.SCALE_SMOOTH)));

        // Sets customized PopupMenu

        // Add statuses options
//        ButtonGroup group = new ButtonGroup();

        // Add 'Available' status option
//        rbAvailable = new JRadioButtonMenuItem("Available");
//        rbAvailable.setIcon(icoDummy);
//        rbAvailable.addActionListener(this);
//        group.add(rbAvailable);
//        menu.add(rbAvailable);
//        index++;

        // Add 'Offline' status option
//        rbOffline = new JRadioButtonMenuItem("Offline");
//        rbOffline.setIcon(icoDummy);
//        rbOffline.addActionListener(this);
//        group.add(rbOffline);
//        menu.add(rbOffline);
//        index++;

        // Add control options
//        menu.insertSeparator(index);
//        menu.add(makeMenuItem("Change avatar", 0, 0, 0, "change_avatar"));
//        index++;
        menu.add(makeMenuItem("Account details", KeyEvent.VK_A, 0, 0, "account_detail"));
        index++;

        // Add logout option
        menu.addSeparator();
        menu.add(makeMenuItem("Logout", KeyEvent.VK_L, KeyEvent.VK_D,
                KeyEvent.CTRL_DOWN_MASK, "logout"));

        return menu;


    }

    /**
     * Creates an Iconized MenuItem based on the input info. Use the other
     * method to create a standard one.
     *
     * @param name The name of the menu item.
     * @param key The Mnemonic KeyEvent for this menu item.
     * @param icon The icon name of this menu item. Cannot be null and must be
     * the same with the icon file name.
     * @param ac The Accelerator KeyEvent for this menu item.
     * @param mask The Mask (Ctrl, Shift or Alt) for this menu item.
     * @param cmd The ActionCommand for this menu item. Cannot be null.
     * @return The created MenuItem
     */
    private JMenuItem makeMenuItem(String name, int key, String icon, int ac,
            int mask, String cmd) {
        JMenuItem m = makeMenuItem(name, key, ac, mask, cmd);

        ImageIcon ic = Util.makeIcon(icon);
        m.setIcon(ic);

        return m;
    }

    /**
     * Creates a non Iconized MenuItem based on the input info.
     *
     * @param name The name of the menu item.
     * @param key The Mnemonic KeyEvent for this menu item.
     * @param ac The Accelerator KeyEvent for this menu item.
     * @param mask The Mask (Ctrl, Shift or Alt) for this menu item.
     * @param cmd The ActionCommand for this menu item. Cannot be null.
     * @return The created MenuItem
     */
    private JMenuItem makeMenuItem(String name, int key, int ac,
            int mask, String cmd) {
        JMenuItem m = new JMenuItem(name, key);

        // Specifies a standard menu item.
        if (ac != 0) {
            m.setAccelerator(KeyStroke.getKeyStroke(ac, mask));
        }
        m.setActionCommand(cmd);
        m.addActionListener(this);

        return m;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        Object source = e.getSource();

        if (cmd.equalsIgnoreCase("about")) {
            new Dialog_About();
        } else if (cmd.equalsIgnoreCase("exit")) {
            try {
                engine.logout();
            } catch (RemoteException ex) {
                Logger.getLogger(MainMenuBar.class.getName()).log(Level.SEVERE,
                        null, ex);
            }
            System.exit(0);
        } else if (cmd.equalsIgnoreCase("available")) {
            rbAvailable.setIcon(icoAvail);
            rbOffline.setIcon(icoDummy);
        } else if (cmd.equalsIgnoreCase("offline")) {
            rbOffline.setIcon(icoOff);
            rbAvailable.setIcon(icoDummy);
        } else if (cmd.equalsIgnoreCase("logout")) {
            System.out.println("logout click");
            try {
                engine.getClient().getView().dispose();
                engine.logout();

                new Dialog_Login(engine);
            } catch (RemoteException ex) {
                Logger.getLogger(MainMenuBar.class.getName()).log(Level.SEVERE,
                        null, ex);
            }

        } else if (cmd.equalsIgnoreCase("conference")) {
            startConference();
        } else if (cmd.equalsIgnoreCase("account_detail")) {
            System.out.println("Account Detail click");
            new Dialog_UserInfo(engine);
        }
    }

    private void startConference() {
        System.out.println("Start Conference");
        Dialog_Conference conf = new Dialog_Conference(engine,
                engine.getClient().getUID());
        engine.getClient().getListConference().put(engine.getClient().getUID() + "" + conf.getConferenceName(), conf);

    }
}
